package com.platform.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.platform.dao.GoodsSpecificationDao;
import com.platform.dao.ProductDao;
import com.platform.entity.GoodsEntity;
import com.platform.entity.GoodsSpecificationEntity;
import com.platform.entity.ProductEntity;
import com.platform.entity.UserEntity;
import com.platform.utils.StringUtils;

/**
 * Service接口
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-30 14:31:21
 */
@Service("productService")
public class ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private GoodsSpecificationDao goodsSpecificationDao;

    public ProductEntity queryObject(Integer id) {
    	ProductEntity product = productDao.queryObject(id);
        String specificationIds = product.getGoodsSpecificationIds();
        StringBuffer specificationValue = new StringBuffer();
        if (!StringUtils.isNullOrEmpty(specificationIds)) {
            String[] arr = specificationIds.split("_");

            for (String goodsSpecificationId : arr) {
                GoodsSpecificationEntity entity = goodsSpecificationDao.queryObject(goodsSpecificationId);
                if (null != entity) {
                    specificationValue.append(" ").append(entity.getValue());
                }
            }
        }
        product.setSpecificationValue(specificationValue.toString());
        return product;
    }

    @Transactional
    public List<ProductEntity> queryList(Map<String, Object> map) {
    	return queryList(map,true,null);
    }
    
    @Transactional
    public List<ProductEntity> queryList(Map<String, Object> map,boolean isShowName,UserEntity currentUser) {
        List<ProductEntity> list = productDao.queryList(map);
        if(!isShowName) {
        	return list;
        }
        List<ProductEntity> result = new ArrayList<>();
        //翻译产品规格
        if (null != list && list.size() > 0) {
            for (ProductEntity item : list) {
            	item.setUser(currentUser);
                String specificationIds = item.getGoodsSpecificationIds();
                String specificationValue = "";
                if (!StringUtils.isNullOrEmpty(specificationIds)) {
                    String[] arr = specificationIds.split("_");

                    for (String goodsSpecificationId : arr) {
                        GoodsSpecificationEntity entity = goodsSpecificationDao.queryObject(goodsSpecificationId);
                        if (null != entity) {
                            specificationValue += entity.getValue() + ";";
                        }
                    }
                }
                item.setSpecificationValue(item.getGoodsName() + " " + specificationValue);
                result.add(item);
            }
        }
        return result;
    }

    public int queryTotal(Map<String, Object> map) {
        return productDao.queryTotal(map);
    }


    /**
     * 根据商品规格初始化产品
     * @param goods
     * @param goodsSpecificationsList
     * @return
     */
    @Transactional
    public void initProduct(GoodsEntity goods, List<List<GoodsSpecificationEntity>> goodsSpecificationsList) {
    	//先删除之前产品
    	productDao.deleteByGoodsId(goods.getId());
    	
    	List<String> goodsSpecificationIdsList = new ArrayList<String>();
    	descartes(goodsSpecificationsList,goodsSpecificationIdsList,0,null);
    	boolean isFirst = true;
    	for(String goodsSpecificationIds:goodsSpecificationIdsList) {
    		ProductEntity product = new ProductEntity();
    		product.setAppExclusivePrice(goods.getAppExclusivePrice());
    		product.setCounterPrice(goods.getCounterPrice());
    		product.setGoodsId(goods.getId());
    		product.setGoodsName(goods.getName());
    		product.setGoodsNumber(goods.getGoodsNumber());
    		product.setGoodsSn(goods.getGoodsSn());
    		product.setGoodsSpecificationIds(goodsSpecificationIds);
    		product.setMarketPrice(goods.getMarketPrice());
    		if(isFirst) {
        		product.setPrimaryProduct(1);
        		isFirst = false;
    		}else {
        		product.setPrimaryProduct(0);
    		}
    		product.setRetailPrice(goods.getRetailPrice());
    		product.setUnitPrice(goods.getUnitPrice());
    		productDao.save(product);
    	}
    }

    private void descartes(List<List<GoodsSpecificationEntity>> dimvalue, List<String> result, int layer, String curstring)
    {
        //大于一个集合时：
        if (layer < dimvalue.size() - 1)
        {
            //大于一个集合时，第一个集合为空
            if (dimvalue.get(layer).size() == 0)
            	descartes(dimvalue, result, layer + 1, curstring);
            else
            {
                for (int i = 0; i < dimvalue.get(layer).size(); i++)
                {
                    StringBuilder s1 = new StringBuilder();
                    if(curstring!=null) {
                        s1.append(curstring).append("_");
                    }
                    s1.append(dimvalue.get(layer).get(i).getId());
                    descartes(dimvalue, result, layer + 1, s1.toString());
                }
            }
        }
        //只有一个集合时：
        else if (layer == dimvalue.size() - 1)
        {
            //只有一个集合，且集合中没有元素
            if (dimvalue.get(layer).size() == 0)
            	result.add(curstring);
            //只有一个集合，且集合中有元素时：其笛卡尔积就是这个集合元素本身
            else
            {
                for (int i = 0; i < dimvalue.get(layer).size(); i++)
                {
                	result.add(curstring +"_"+ dimvalue.get(layer).get(i).getId());
                }
            }
        }
    }
    
    @Transactional
    public int save(ProductEntity product) {
        return productDao.save(product);
    }

    public int update(ProductEntity product) {
        if (StringUtils.isNullOrEmpty(product.getGoodsSpecificationIds())){
            product.setGoodsSpecificationIds("");
        }
        return productDao.update(product);
    }

    public int delete(Integer id) {
        return productDao.delete(id);
    }

    public int deleteBatch(Integer[] ids) {
        return productDao.deleteBatch(ids);
    }
}
